// 格式化字符串
function formatNumber(num, pattern) {

    var strarr = num ? num.toString().split('.') : ['0'];
    var fmtarr = pattern ? pattern.split('.') : [''];
    var retstr = '';

    // 整数部分  
    var str = strarr[0];
    var fmt = fmtarr[0];
    var i = str.length - 1;
    var comma = false;
    for (var f = fmt.length - 1; f >= 0; f--) {
        switch (fmt.substr(f, 1)) {
            case '#':
                if (i >= 0) retstr = str.substr(i--, 1) + retstr;
                break;
            case '0':
                if (i >= 0) retstr = str.substr(i--, 1) + retstr;
                else retstr = '0' + retstr;
                break;
            case ',':
                comma = true;
                retstr = ',' + retstr;
                break;
        }
    }
    if (i >= 0) {
        if (comma) {
            var l = str.length;
            for (; i >= 0; i--) {
                retstr = str.substr(i, 1) + retstr;
                if (i > 0 && ((l - i) % 3) == 0) retstr = ',' + retstr;
            }
        }
        else retstr = str.substr(0, i + 1) + retstr;
    }

    retstr = retstr + '.';
    // 处理小数部分  
    str = strarr.length > 1 ? strarr[1] : '';
    fmt = fmtarr.length > 1 ? fmtarr[1] : '';
    i = 0;
    for (var f = 0; f < fmt.length; f++) {
        switch (fmt.substr(f, 1)) {
            case '#':
                if (i < str.length) retstr += str.substr(i++, 1);
                break;
            case '0':
                if (i < str.length) retstr += str.substr(i++, 1);
                else retstr += '0';
                break;
        }
    }
    return retstr.replace(/^,+/, '').replace(/\.$/, '');
}
/*//拖放动态绘制函数。
function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData)
{
    var sGuid = pData.Guid;
    mxOcx.SetEventRet(0);
    // 当前拖放位置点.
    var curPt = mxOcx.NewPoint();
    curPt.x = dX;
    curPt.y = dY;

	if (sGuid == "DrawComment")
	{
	    DynWorldDrawComment(pData, pWorldDraw, curPt);
		mxOcx.SetEventRet(1);
    }
    else if (sGuid == "DrawComment2") {
        DynWorldDrawComment2(pData, pWorldDraw, curPt);
        mxOcx.SetEventRet(1);
    }
    else if (sGuid == "DrawCircle")
    {
        var pt1 = pData.GetPoint("pt1");
        var dR = curPt.DistanceTo(pt1) * 0.5;
        var vec = curPt.SumVector(pt1);
        vec.Mult(0.5);
        pt1.Add(vec);
        pWorldDraw.DrawCircle(pt1.x, pt1.y, dR);
    }
}*/
/*//交互绘制批注时的动态绘制1
function DynWorldDrawComment2(pCustomEntity, pWorldDraw, curPt) {

	// 得到绘制参数.
	var sText = pCustomEntity.GetString("Text");
	var dTextHeight = pCustomEntity.GetDouble("TextHeight");
	var iEdgeNumber = pCustomEntity.GetLong("EdgeNumber");
	var dShapRadius = pCustomEntity.GetDouble("ShapRadius");
	var basePoint = pCustomEntity.GetPoint("BasePoint");

	// 创建一个批注对象.
	var comment = mxOcx.NewEntity("IMxDrawComment");
	comment.Text = sText;
	comment.TextHeight = dTextHeight;
	comment.EdgeNumber = iEdgeNumber;
	comment.ShapRadius = dShapRadius;
	comment.basePoint = basePoint;
	comment.Position = curPt;

	// 设置文字样式。
	pWorldDraw.TextStyle = "MyCommentFont";

	// 动态绘制.
	pWorldDraw.DrawEntity(comment);
}*/
/*// 自定义实体绘制函数
function ExplodeFun(pCustomEntity, pWorldDraw, txt) {
	var mxOcx = document.all.item("drawingId");
	mxOcx.AddTextStyle1("MyTextStyle", "tssdeng.shx", "hztxt.shx", 1);
    
    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {
        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;
        var tpt = pCustomEntity.GetPoint("tpt");
        if (tpt == null)
            return;
        var txt = pCustomEntity.GetString("infoTxt");
        if (txt == null)
            return;
        var articleType = pCustomEntity.GetString("articleType");
        if (articleType == 1) {
        	articleType = "强条";
        }
        else {
        	articleType = "";
        }
        var draNo = pCustomEntity.GetString("draNo");
        var ztdx = pCustomEntity.GetString("ztdx");
        var Author = articleType + "  图号："+draNo;
        if (Author == null)
            return;
        var mxUtility = mxOcx.NewUtility();
        pWorldDraw.SetColorIndex(1);
        pWorldDraw.TextStyle="MyTextStyle";
        pWorldDraw.LineWidth = 1;
        pWorldDraw.DrawLine(stp.x, stp.y, stp.x, ept.y);
        pWorldDraw.DrawLine(stp.x, stp.y, ept.x, stp.y);
        pWorldDraw.DrawLine(ept.x, ept.y, ept.x, stp.y);
        pWorldDraw.DrawLine(ept.x, ept.y, stp.x, ept.y);

        pWorldDraw.DrawLine(ept.x, ept.y, tpt.x, tpt.y);
        
        pWorldDraw.DrawText(tpt.x, tpt.y + 100, txt, parseInt(ztdx) , 0,
    			0, 1);
        pWorldDraw.DrawText(tpt.x, tpt.y - parseInt(ztdx)-80, Author, parseInt(ztdx), 0,
    			0, 1);

        var tmpText = mxOcx.NewEntity("IMxDrawText");
        tmpText.TextString = txt;
        tmpText.Height = parseInt(ztdx);

        var txtBox = tmpText.GetBoundingBox2();
        if (txtBox != null) {
            var pt1 = txtBox.Item(0);
            var pt2 = txtBox.Item(1);

            var txtWidth = pt2.x - pt1.x;

            pWorldDraw.DrawLine(tpt.x, tpt.y, tpt.x + txtWidth, tpt.y);
        }
        mxOcx.SetEventRet(1);

    }
}*/
/*// 返回自定义实体夹点
function GetGripPointsFun(pCustomEntity) {
	var mxOcx = document.all.item("drawingId");
    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {
        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;

        var tpt = pCustomEntity.GetPoint("tpt");
        if (tpt == null)
            return;      
  
        var ret = mxOcx.NewResbuf();


        ret.AddPoint(stp);
        ret.AddPoint(ept);
        ret.AddPoint(tpt);

        var midPt = mxOcx.NewPoint();
        midPt.x = stp.x + (ept.x - stp.x) / 2.0;
        midPt.y = stp.y + (ept.y - stp.y) / 2.0;
        ret.AddPoint(midPt);
        mxOcx.SetEventRetEx(ret);
    }
}*/
// 移动自定义实体夹点
function MoveGripPointsFun(pCustomEntity, lGridIndex, dOffsetX, dOffsetY) {
	var mxOcx = document.all.item("drawingId");
    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {
        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;

        var tpt = pCustomEntity.GetPoint("tpt");
        if (tpt == null)
            return;
        
        var randomid = pCustomEntity.GetString("randomid");
        if (randomid == null)
        	return;
        
        if (lGridIndex == 1) {
            ept.x = ept.x + dOffsetX;
            ept.y = ept.y + dOffsetY;
            pCustomEntity.SetPoint("ept", ept);
        }
        else if (lGridIndex == 3) {
            // 中点移动.
            ept.x = ept.x + dOffsetX;
            ept.y = ept.y + dOffsetY;
            pCustomEntity.SetPoint("ept", ept);

            stp.x = stp.x + dOffsetX;
            stp.y = stp.y + dOffsetY;
            pCustomEntity.SetPoint("spt", stp);

            tpt.x = tpt.x + dOffsetX;
            tpt.y = tpt.y + dOffsetY;
            pCustomEntity.SetPoint("tpt", tpt);
        }

        if (lGridIndex == 0) {
            stp.x = stp.x + dOffsetX;
            stp.y = stp.y + dOffsetY;
            pCustomEntity.SetPoint("spt", stp);
        }
        if (lGridIndex == 2) {
            tpt.x = tpt.x + dOffsetX;
            tpt.y = tpt.y + dOffsetY;
            pCustomEntity.SetPoint("tpt", tpt);
        }
        var zuobiao = stp.x + "," + stp.y + "," + stp.z;
		zuobiao += "," + ept.x + "," + ept.y + "," + ept.z;
		zuobiao += "," + tpt.x + "," + tpt.y + "," + tpt.z;
		mxOcx.SetEventRet(1);
		//console.log(randomid+"=="+zuobiao);
		/*jQuery.ajax({
			type : 'post',
			url : updateZuobiaoUrl,
			data : {
				"zuobiao" : zuobiao,
				"randomid" : randomid	
				},			
			dataType : "json",
			error : function(value) {
				var errormsg = value.msg;
				if (errormsg == "0")
					ds.dialog.alert('移动失败');
			},
			success : function(value) {}
		});*/   

		//将坐标信息信息放到一个数组中，原坐标和新坐标配对，定时器500毫秒轮询比较如果坐标有变化，则保存坐标到数据库。
		var ifExistZuobiao = false;
		for (var i = 0; i < zuobiaoArr.length; i++) {
		    if (zuobiaoArr[i].randomid == randomid) {
		        ifExistZuobiao = true;
		        zuobiaoArr[i].oldzuobiao = zuobiaoArr[i].newzuobiao;
		        zuobiaoArr[i].newzuobiao = zuobiao;
		    }
		}
		if (ifExistZuobiao == false) {
		    var zuobiaoTemp = {};
		    zuobiaoTemp.randomid = randomid;
		    zuobiaoTemp.oldzuobiao = "";
		    zuobiaoTemp.newzuobiao = zuobiao;
		    zuobiaoArr.push(zuobiaoTemp);
		}
    }
}

var zuobiaoArr = [];
window.setInterval(refreshZuobiao, 500); 
function refreshZuobiao() {
    for (var i = 0; i < zuobiaoArr.length; i++) {
    	if(zuobiaoArr[i]==""){
    		continue;
    	}
        var randomid = zuobiaoArr[i].randomid;
        var oldzuobiao = zuobiaoArr[i].oldzuobiao;
        var newzuobiao = zuobiaoArr[i].newzuobiao;
        if (oldzuobiao == "0" || oldzuobiao != newzuobiao) {
            zuobiaoArr[i]="";
            jQuery.ajax({
                type: 'post',
                url: updateZuobiaoUrl,
                data: {
                    "zuobiao": newzuobiao,
                    "randomid": randomid
                },
                dataType: "json",
                error: function (value) {
                        ds.dialog.alert('移动失败');
                },
                success: function (value) {}
            });
        }
    }
} 



window.console = window.console || (function () {
    var c ={}; 
　　 c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile= c.clear = c.exception = c.trace = c.assert = function(){};
    return c;
})();
      
/*// 变换自定义实体
function TransformByFun(pCustomEntity, pMatXform) {
	var mxOcx = document.all.item("drawingId");
    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {
        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;
        var tpt = pCustomEntity.GetPoint("tpt");
        if (tpt == null)
            return;
        tpt.TransformBy(pMatXform);
        stp.TransformBy(pMatXform);
        ept.TransformBy(pMatXform);

        pCustomEntity.SetPoint("spt", stp);
        pCustomEntity.SetPoint("tpt", tpt);
        pCustomEntity.SetPoint("ept", ept);
        mxOcx.SetEventRet(1);
    }
}*/
/*// 返回自定义实体最小外包
function GetGeomExtentsFun(pCustomEntity) {
	var mxOcx = document.all.item("drawingId");
    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {
        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;
        var tpt = pCustomEntity.GetPoint("tpt");
        if (tpt == null)
            return;


        var ret = mxOcx.NewResbuf();

        ret.AddPoint(stp);
        ret.AddPoint(ept);
        ret.AddPoint(tpt);

        mxOcx.SetEventRetEx(ret);
    }
}*/

function GetOsnapPointsFun(pCustomEntity, lOsnapMode, dPickPointX, dPickPointY, dLastPointX, dLastPointY) {
    // 		enum OsnapMode         { kOsModeEnd          = 1,
    // 			kOsModeMid          = 2,
    // 			kOsModeCen          = 3,
    // 			kOsModeNode         = 4,
    // 			kOsModeQuad         = 5, 
    // 			kOsModeIns          = 7,
    // 			kOsModePerp         = 8,
    // 			kOsModeTan          = 9,
    // 			kOsModeNear         = 10,
    // 			kOsModeInt          = 11
    // 		};

    var sGuid = pCustomEntity.Guid;
    if (sGuid == "TestMxCustomEntity") {

        if (!pCustomEntity.IsHave("ept"))
            return;

        var stp = pCustomEntity.GetPoint("spt");
        if (stp == null)
            return;

        var ept = pCustomEntity.GetPoint("ept");
        if (ept == null)
            return;


        if (lOsnapMode == 1) {
            // 端点捕捉。
            var pickPoint = mxOcx.NewPoint();

            pickPoint.x = dPickPointX;
            pickPoint.y = dPickPointY;

            var dDis1 = pickPoint.DistanceTo(stp);
            var dDis2 = pickPoint.DistanceTo(ept);

            var ret = mxOcx.NewResbuf();

            if (dDis1 < dDis2) {
                ret.AddDouble(stp.x);
                ret.AddDouble(stp.y);
            }
            else {
                ret.AddDouble(ept.x);
                ret.AddDouble(ept.y);

            }

            mxOcx.SetEventRetEx(ret);
        }
        else if (lOsnapMode == 10) {
            // 最近点捕捉.
            var pickPoint = mxOcx.NewPoint();

            pickPoint.x = dPickPointX;
            pickPoint.y = dPickPointY;


            var line = mxOcx.NewEntity("IMxDrawLine");
            line.EndPoint = ept;
            line.StartPoint = stp;

            var closePoint = line.GetClosestPointTo2(pickPoint, false);

            if (closePoint != null) {
                var ret = mxOcx.NewResbuf();
                ret.AddDouble(closePoint.x);
                ret.AddDouble(closePoint.y);
                mxOcx.SetEventRetEx(ret);
            }
        }

    }
}